package com.xinqi.common.base.utils.reflects;

import org.joor.Reflect;
import org.joor.ReflectException;

import java.lang.reflect.Field;
import java.util.Objects;

/**
 * 反射工具
 *
 * @author linkai
 * @date 2022/11/25
 **/
public class ReflectUtils {

    public static Field getDeclaredFields(Class<?> clazz, String filedName) {
        for (Field field : clazz.getDeclaredFields()) {
            if (Objects.equals(field.getName(), filedName)) {
                return field;
            }
        }
        return null;
    }

    public static boolean setFiled(Object o, Field field, Object value) {
        try {
            Reflect.on(o).set(field.getName(), value);
            return true;
        } catch (ReflectException e) {
            return false;
        }
    }

    public static Object getFiledValue(Object object, Field f) {
        return Reflect.on(object).field(f.getName()).get();
    }
}
